# Copyright (c) 2020 Presto Labs Pte. Ltd.
# Author: fengyang, donggu

import requests
from coin2.exchange.product_info import ProductInfoDefault
from coin2.exchange.symbology import SymbologyInfo


def get_quote_currency(raw_currency):
  currency_map = {
    "USD_Stable_Coin": "USD",
  }
  if raw_currency in currency_map:
    return currency_map[raw_currency]
  else:
    return raw_currency


def generate():
  mea = 'Spot.Crypto.v1' # for feed
  me = 'Spot.Crypto'
  default = ProductInfoDefault()
  symbol_info = SymbologyInfo()
  data = requests.get("https://api.crypto.com/v2/public/get-instruments").json()['result']['instruments']

  pis = []
  for info in data:
    native_symbol = info['instrument_name']
    native_base = info['base_currency']
    native_quote = get_quote_currency(info['quote_currency'])
    base = symbol_info.native_to_norm_currency(mea, native_base)
    quote = symbol_info.native_to_norm_currency(mea, native_quote)
    symbol = "%s-%s" % (base, quote)

    pi = dict(
        symbol=symbol,
        base=base,
        quote=quote,
        native_symbol=native_symbol,
        native_base=native_base,
        native_quote=native_quote,
        price_ticksize= 1.0 / pow(10, info['price_decimals']),
        qty_ticksize= 1.0 / pow(10, info['quantity_decimals']),
        min_qty=info['min_quantity'],
    )

    pi = {**pi, **default.product_info(me, symbol)}
    pis.append(pi)

  pis = sorted(pis, key=lambda x: x['symbol'])
  return pis


if __name__ == "__main__":
  import json
  print(json.dumps(generate(), indent=2))
